#!/bin/bash

set -e
set -x

bypy download kafka_2.13-2.4.1.tgz

sh render.sh

iplist_path=config
i=0
while read -r line
do
    case $line in
        *'#'*)
            continue
        ;;
        *'end'*)
            break
        ;;
        *)
            IFS=',' read -ra parts <<< "$line"
        ;;
    esac
    
    pass="${parts[0]}" 
    address="${parts[1]}"
    hostname="${parts[2]}"
    if [ $address ];then
        sshpass -p$pass scp kafka_2.13-2.4.1.tgz $address:/opt/
        sshpass -p$pass ssh -n $address tar xf /opt/kafka_2.13-2.4.1.tgz -C /opt/
        sshpass -p$pass ssh -n $address ln -fs /opt/kafka_2.13-2.4.1 /opt/kafka
        sshpass -p$pass ssh -n $address 'echo "JAVA=$JAVA_HOME" >> /opt/kafka/env'
        sshpass -p$pass ssh -n $address 'echo "PATH=$PATH" >> /opt/kafka/env'
        sshpass -p$pass scp server.properties $address:/opt/kafka/config/
        sshpass -p$pass ssh -n $address sed -i "s/{broker_id}/$i/" /opt/kafka/config/server.properties
        sshpass -p$pass scp kafka.service $address:/usr/lib/systemd/system/
        sshpass -p$pass ssh -n $address systemctl daemon-reload
        sshpass -p$pass ssh -n $address systemctl enable --now kafka        
        let i=i+1
    fi
done < $iplist_path

set +x
set +e
